package com.atwu.wl2022.controller;

import com.atwu.wl2022.entity.Article;
import com.atwu.wl2022.entity.Comment;
import com.atwu.wl2022.entity.User;
import com.atwu.wl2022.mapper.CommentMapper;
import com.atwu.wl2022.service.IArticleService;
import com.atwu.wl2022.service.ICommentService;
import com.atwu.wl2022.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @Description: 评论控制器
 * @author wulejay
 */
@Controller
@RequestMapping("/comment")
@Slf4j
public class CommentController {

	@Autowired
	CommentMapper commentMapper;

	@Autowired
	ICommentService iCommentService;

	@Autowired
	IUserService iUserService;

	@Autowired
	IArticleService iArticleService;

	/**
	 * 增加文章和留言的一级评论
	 * @param articleId  文章id
	 * @param userId 用户Id
	 * @param content 评论内容
	 */
	@PostMapping("/replayArticle")
	public String replayArticle(String articleId ,String  userId, String content){
		//1、封装评论信息到数据库中
		// articleId : 0为对网站的回复，有值为文章回复  pid:默认为0，即根评论   （拓展===》isDelete：是否删除，默认为0（否）)
		log.info("封装的回复信息为，用户id：{}，文章id：{}，评论内容：{}",userId,articleId,content);
		int userByid = Integer.parseInt(userId);
		int articleById = Integer.parseInt(articleId);
		//将评论信息分封装，插入数据库
		Comment comment = new Comment( userByid, articleById, content);
		commentMapper.insert(comment);
		/**
		 * 当对文章回复时
		 */
		if(articleById!=0){
			//插入数据库之后，将文章表中commentSize加1
			UpdateWrapper wrapper = new UpdateWrapper();
			wrapper.eq("id",articleId);
			wrapper.setSql("commentSize = commentSize + 1");
			iArticleService.update(wrapper);
		}
		//跳转到局部刷新页面
		return "redirect:/comment/comments/"+ articleId;
	}

	/**
	 * * 增加文章的二级和多级评论
	 * @param remarkId  评论的用户id
	 * @param replyContent 回复内容
	 * @param targetCommentId 被评论的评论id
	 * @param articleId1 文章id
	 */
	@PostMapping("/replayArticle2")
	public String replayArticle2(String remarkId ,String  replyContent, String targetCommentId,String articleId){
		//1、封装二级评论信息到数据库中
		// articleId : 0为对网站的回复，有值为文章回复  pid:默认为0，即根评论   （拓展===》isDelete：是否删除，默认为0（否）)
		log.info("封装的回复信息为，评论的用户id：{}，文章id：{}，回复内容：{}，被评论的评论id：{}",remarkId,articleId,replyContent,targetCommentId);
		int uId = Integer.parseInt(remarkId);
		int aId = Integer.parseInt(articleId);
		int pid = Integer.parseInt(targetCommentId);
		//将评论信息封装
		Comment comment = new Comment(uId, aId, replyContent,pid);
		//找到接收的pid对象
		Comment pComment = iCommentService.getById(pid);
		//如果当前回复的父级有祖宗，那么就设置相同的祖宗
		if (pComment.getOriginId() != null){
			//将父评论的祖宗id赋值给当前评论的祖宗id
			comment.setOriginId(pComment.getOriginId());
		}else {
			//否则就将父评论id设置为当前评论的祖宗id
			comment.setOriginId(comment.getPid());
		}
		//插入数据库，局部刷新页面
		commentMapper.insert(comment);
		if(aId!=0){
			//插入数据库之后，将文章表中commentSize加1
			UpdateWrapper wrapper = new UpdateWrapper();
			wrapper.eq("id",aId);
			wrapper.setSql("commentSize = commentSize+ 1");
			iArticleService.update(wrapper);
		}
		return "redirect:/comment/comments/"+ articleId;
	}

	/**
	 * 按照时间顺序查询文章和留言评论列表
	 * @param articleId 文章id
	 * @param session 域
	 * @return 局部刷新位置
	 */
	@GetMapping("/comments/{articleId}")
	public String listComment(@PathVariable("articleId") Integer articleId, HttpSession session) {
		List<Comment> originList = iCommentService.listComment(articleId);
		log.info("id为{}的文章，已评论信息",articleId);
		session.setAttribute("originList",originList);
		if(articleId!=0){
			return "read::list";
		}else {
			return "message::messageList";
		}
	}
}
